package com.dc.adaptor.step;

import java.util.Date;

import org.pentaho.di.trans.Trans;
import org.pentaho.di.trans.TransMeta;
import org.pentaho.di.trans.step.StepDataInterface;
import org.pentaho.di.trans.step.StepMeta;
import org.pentaho.di.trans.step.StepMetaInterface;

import com.dc.adaptor.Adaptor;
import com.dc.adaptor.container.AdaptorContainer;
import com.dc.adaptor.persistance.DatabaseHandler;
import com.dc.adaptor.persistance.PersistenceHandler;
import com.dc.adaptor.persistance.bean.SipProtocol;
import com.dc.adaptor.transport.TransportMeta;
import com.dc.adaptor.transport.addressconvertor.AddressConvertorFactory;
import com.dc.di.util.FaultInfoRecord;
import com.dc.di.util.GUIDGen;
import com.dc.trans.steps.rowlevellog.logprocess.AbstractTransStep;

public abstract class AdaptorBaseStep extends AbstractTransStep implements Adaptor {
	
	private Date starttime = new Date();
	
	private SipProtocol sipProtocol;
	
	protected PersistenceHandler persistenceHandler;
	public AdaptorBaseStep(StepMeta stepMeta,
			StepDataInterface stepDataInterface, int copyNr,
			TransMeta transMeta, Trans trans) {
		super(stepMeta, stepDataInterface, copyNr, transMeta, trans);
	}
	@Override
	public PersistenceHandler getPersistenceHandler(){
		return persistenceHandler;
	}
	@Override
	public long getInputLines() {
		return 0;
	}

	@Override
	public long getOutputLines() {
		return 0;
	}
	@Override
	public boolean init(StepMetaInterface smi, StepDataInterface sdi) {
		persistenceHandler = new DatabaseHandler(this.getTransMeta().getRepository());	
		if(smi instanceof TransportMeta){
			SipProtocol sipProtocol = new SipProtocol();
			sipProtocol.setId(GUIDGen.getGUIDString());
			sipProtocol.setType(getType());
			sipProtocol.setAddress(AddressConvertorFactory.getAddressMeta(getType()).getAddress((TransportMeta) smi));
			sipProtocol.setJobInstanceId(getJobInstanceId());
			try {
				persistenceHandler.saveProtocol(sipProtocol);
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
			this.sipProtocol = sipProtocol;
		}
		init();
		return super.init(smi, sdi);
	}
	@Override
	public void dispose(StepMetaInterface smi, StepDataInterface sdi) {
		stop();	
		super.dispose(smi, sdi);
	}
	@Override
	public void init() {		
		AdaptorContainer.register(this);
	}

	@Override
	public void stop() {
		AdaptorContainer.remove(this);
		if(persistenceHandler!=null)persistenceHandler.close();
	}

	@Override
	public long getProcessCount() {
		return getProcessed();
	}

	@Override
	public Date getStarttime() {
		return starttime;
	}


	public String getJobName(){
		if(this.getTrans().getParentJob()!= null){
			return this.getTrans().getParentJob().getJobname();
		}
		return null;
	}
	
	public String getJobInstanceId(){
		if(this.getTrans().getParentJob()!= null){
			return this.getTrans().getParentJob().getInstanceId();
		}
		return null;
	}
	
	public void recordFault(String exName,String exDescription){
		FaultInfoRecord.getInstance().recordFaultlog(exName, exDescription);
	}
	
	public SipProtocol getSipProtocol(){
		return sipProtocol;	
	}
	
	protected static String getSystemConfig(String key,String defaultValue){
		String config = System.getProperty(key);
		if(config!=null){
			return config;
		}
		return defaultValue;
	}
}
